package com.meilai.project.util;

import com.meilai.project.entity.BaseEntity;
import com.meilai.project.vo.BaseTreeVO;
import com.meilai.project.vo.TreeVO;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 张驰
 * @date 2020/9/14 14:44
 */
public class TreeUtil {
    public static void TreeForEachUseSort(Map<Long, List<TreeVO>> collect, TreeVO treeMenuNode) {
        List<TreeVO> treeMenuNodes = collect.get(treeMenuNode.getId());
        if(collect.get(treeMenuNode.getId())!=null){
            //排序
            treeMenuNodes.sort((u1, u2) -> u1.getSort().compareTo(u2.getSort()));
            treeMenuNodes.stream().sorted(Comparator.comparing(TreeVO::getSort)).collect(Collectors.toList());
            treeMenuNode.setChildren(treeMenuNodes);
            treeMenuNode.getChildren().forEach(t->{
                TreeForEachUseSort(collect,t);
            });
        }
    }

    public static void TreeForEach(Map<Long, List<TreeVO>> collect, TreeVO treeMenuNode) {
        List<TreeVO> treeMenuNodes = collect.get(treeMenuNode.getId());
        if(collect.get(treeMenuNode.getId())!=null){
            treeMenuNode.setChildren(treeMenuNodes);
            treeMenuNode.getChildren().forEach(t->{
                TreeForEach(collect,t);
            });
        }
    }

    public static <T extends BaseTreeVO> void TreeForEachUseSort(Map<Long, List<T>> collect, T treeMenuNode) {
        List<T> treeMenuNodes = collect.get(treeMenuNode.getId());
        if(collect.get(treeMenuNode.getId())!=null){
            //排序
            treeMenuNodes.sort(Comparator.comparing(BaseEntity::getSort));
            treeMenuNode.setChildren(treeMenuNodes);
            treeMenuNode.getChildren().forEach(t->{
                TreeForEachUseSort(collect, (T) t);
            });
        }
    }

    public static <T extends BaseTreeVO> void TreeForEach(Map<Long, List<T>> collect, T treeMenuNode) {
        List<T> treeMenuNodes = collect.get(treeMenuNode.getId());
        if(collect.get(treeMenuNode.getId())!=null){
            treeMenuNode.setChildren(treeMenuNodes);
            treeMenuNode.getChildren().forEach(t->{
                TreeForEach(collect, (T) t);
            });
        }
    }
}
